package kt.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Random;
import java.util.Vector;

public class StringUtil {
	static final char NINE = '9';
	static final char ZERO = '0';
	static final char CH_a = 'a';
	static final char CH_z = 'z';
	static final char CH_A = 'A';
	static final char CH_Z = 'Z';
	static final String[] seperators = { " ", ".", ",", "-", "_", "=" };

	private static char[] charArray = null;
	private static Random random = null;

	static {
		int numOfChars = 26;
		int numOfDigits = 10;

		random = new Random();

		charArray = new char[numOfChars + numOfDigits];
		for (int i = 0; i < numOfChars; i++) {
			charArray[i] = (char) (65 + i);
		}
		for (int i = 0; i < numOfDigits; i++)
			charArray[(numOfChars + i)] = (char) (48 + i);
	}

	public static String appendString(String oldS, int pos, String s) {
		return oldS.substring(0, pos) + s + oldS.substring(pos);
	}

	public static String replaceCharAt(String s, int pos, char c) {
		StringBuffer buf = new StringBuffer(s);
		buf.setCharAt(pos, c);
		return buf.toString();
	}

	public static String replaceChar(String s, char a, String b) {
		if (s == null)
			return null;

		StringBuffer newString = new StringBuffer();
		for (int i = 0; i < s.length(); i++) {
			char cur = s.charAt(i);
			if (cur == a)
				newString.append(b);
			else {
				newString.append(cur);
			}
		}
		return newString.toString();
	}

	public static String removeChar(String s, char c) {
		if (s == null)
			return null;

		StringBuffer newString = new StringBuffer();
		for (int i = 0; i < s.length(); i++) {
			char cur = s.charAt(i);
			if (cur != c)
				newString.append(cur);
		}
		return newString.toString();
	}

	public static String removeCharAt(String s, int pos) {
		StringBuffer buf = new StringBuffer(s.length() - 1);
		buf.append(s.substring(0, pos)).append(s.substring(pos + 1));
		return buf.toString();
	}

	public static String removeSpecialCharsInFront(String s) {
		if (s == null)
			return null;
		String result = "";

		for (int i = 0; i < s.length(); i++) {
			char currChar = s.charAt(i);
			if (((currChar < '0') || (currChar > '9'))
					&& ((currChar < 'a') || (currChar > 'z'))
					&& ((currChar < 'A') || (currChar > 'Z')))
				continue;
			result = s.substring(i);
			break;
		}

		return result;
	}

	public static String removeSpecialCharsInString(String s) {
		if (s == null)
			return null;
		StringBuffer buffer = new StringBuffer();

		for (int i = 0; i < s.length(); i++) {
			char ch = s.charAt(i);
			if (((ch < '0') || (ch > '9')) && ((ch < 'a') || (ch > 'z'))
					&& ((ch < 'A') || (ch > 'Z')))
				continue;
			buffer.append(ch);
		}

		return buffer.toString();
	}

	public static String only2SpaceBetweenWords(String text) {
		if (text == null)
			return null;

		StringBuffer buffer = new StringBuffer();
		boolean lastCharIsSpace = false;
		for (int i = 0; i < text.length(); i++) {
			char ch = text.charAt(i);
			if (ch == ' ') {
				if (lastCharIsSpace) {
					continue;
				}
				lastCharIsSpace = true;
			} else if (lastCharIsSpace) {
				lastCharIsSpace = false;
			}
			buffer.append(ch);
		}
		return buffer.toString();
	}

	public static ArrayList<String> parseString(String text, String seperator) {
		ArrayList<String> vResult = new ArrayList<String>();
		if ((text == null) || ("".equals(text))) {
			return vResult;
		}
		String tempStr = text.trim();
		String currentLabel = null;

		int index = tempStr.indexOf(seperator);
		while (index != -1) {
			currentLabel = tempStr.substring(0, index).trim();

			if (!"".equals(currentLabel))
				vResult.add(currentLabel);
			tempStr = tempStr.substring(index + 1);
			index = tempStr.indexOf(seperator);
		}

		currentLabel = tempStr.trim();
		if (!"".equals(currentLabel))
			vResult.add(currentLabel);
		return vResult;
	}

	public static Collection<String> parseString(String text) {
		Vector<String> vResult = new Vector<String>();
		if ((text == null) || ("".equals(text))) {
			return vResult;
		}
		String tempStr = text.trim();
		String currentLabel = null;

		int index = getNextIndex(tempStr);
		while (index != -1) {
			currentLabel = tempStr.substring(0, index).trim();

			if (!"".equals(currentLabel))
				vResult.addElement(currentLabel);
			tempStr = tempStr.substring(index + 1);
			index = getNextIndex(tempStr);
		}

		currentLabel = tempStr.trim();
		if (!"".equals(currentLabel))
			vResult.addElement(currentLabel);
		return vResult;
	}

	private static int getNextIndex(String text) {
		int index = 0;
		int newIdx = 0;
		boolean hasOne = false;
		for (int i = 0; i < seperators.length; i++) {
			newIdx = text.indexOf(seperators[i]);
			if (!hasOne) {
				if (newIdx != -1) {
					index = newIdx;
					hasOne = true;
				}
			} else {
				if ((newIdx == -1) || (newIdx >= index))
					continue;
				index = newIdx;
			}
		}

		if (!hasOne)
			index = -1;
		return index;
	}

	public static Collection<String> parseStringEx(String text) {
		Vector<String> vResult = new Vector<String>();
		if ((text == null) || ("".equals(text))) {
			return vResult;
		}
		String tempStr = text.trim();
		String currLabel = "";
		char currChar = '\000';
		for (int i = 0; i < tempStr.length(); i++) {
			currChar = tempStr.charAt(i);
			if (((currChar >= '0') && (currChar <= '9'))
					|| ((currChar >= 'a') && (currChar <= 'z'))
					|| ((currChar >= 'A') && (currChar <= 'Z'))) {
				currLabel = currLabel + currChar;
			} else if (currLabel.length() > 0) {
				vResult.add(currLabel);
				currLabel = new String("");
			}
		}

		if (currLabel.length() > 0) {
			vResult.add(currLabel);
		}
		return vResult;
	}

	public static boolean isNumberic(String sNumber) {
		if ((sNumber == null) || ("".equals(sNumber))) {
			return false;
		}
		char ch_max = '9';
		char ch_min = '0';

		for (int i = 0; i < sNumber.length(); i++) {
			char ch = sNumber.charAt(i);
			if ((ch < ch_min) || (ch > ch_max)) {
				return false;
			}
		}
		return true;
	}

	public static String generateRandomString(int length) {
		char[] ch = new char[length];
		for (int i = 0; i < length; i++)
			ch[i] = charArray[random.nextInt(charArray.length)];
		return new String(ch);
	}

	public static void main(String[] args) {
		System.out.println(appendString("0123456", "0123".length(), " "));
	}
}